﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChinessChess
{
    class CMa:CQuanCo
    {
        private int new_Index, old_Index;
        
        public CMa() { 

        }

        public CMa(COCo[] chess, int newIndex, int oldIndex, ChessColor color)
            : base(chess, color)
        {
            this.new_Index = newIndex;
            this.old_Index = oldIndex;
        }
        

        public override bool CheckIndex(){

            return isBool();
        }

        //Xet xem co quan nao can nuoc di cua con ma khong
        private Boolean isBool() {
            int vt = flag[new_Index].Dong - flag[old_Index].Dong;
            vt = Math.Abs(vt);
            if (vt == 1){
                int cot = flag[new_Index].Cot - flag[old_Index].Cot;
                if (cot > 0){
                    if (flag[old_Index + 1].State == false) {
                        return true;
                    }
                }
                else {
                    if (flag[old_Index - 1].State == false){
                        return true;
                    }
                }
            }
            else {
                int dong = flag[new_Index].Dong - flag[old_Index].Dong;
                if (dong > 0){
                    if (flag[old_Index + 9].State == false){
                        return true;
                    }
                }
                else{
                    if (flag[old_Index - 9].State == false){
                        return true;
                    }
                }
            }
            return false;
        }

        public override bool CheckPoint(){
            int dong = flag[new_Index].Dong - flag[old_Index].Dong;
            int cot = flag[new_Index].Cot - flag[old_Index].Cot;
            if ((Math.Abs(dong) == 1 && Math.Abs(cot) == 2) || (Math.Abs(dong) == 2 && Math.Abs(cot) == 1))
                return true;
            return false;
        }

        public override bool KiemTraChieuTuong(int gIndex, int qIndex)
        {
            Boolean isCheck=base.KiemTraChieuTuong(gIndex, qIndex);
            int dong = flag[gIndex].Dong - flag[qIndex].Dong;
            int cot = flag[gIndex].Cot - flag[qIndex].Cot;
            if ((Math.Abs(dong) == 1 && Math.Abs(cot) == 2) || (Math.Abs(dong) == 2 && Math.Abs(cot) == 1))
            {
                isCheck = true;
            }
            if (isCheck) {
                int vt = flag[new_Index].Dong - flag[old_Index].Dong;
                vt = Math.Abs(vt);
                if (vt == 1)
                {
                     
                    if (cot > 0)
                    {
                        if (flag[old_Index + 1].State == false)
                        {
                            isCheck = false;
                        }
                    }
                    else
                    {
                        if (flag[old_Index - 1].State == false)
                        {
                            isCheck = false;
                        }
                    }
                }
                else
                {
                     
                    if (dong > 0)
                    {
                        if (flag[old_Index + 9].State == false)
                        {
                            isCheck = false;
                        }
                    }
                    else
                    {
                        if (flag[old_Index - 9].State == false)
                        {
                            isCheck = false;
                        }
                    }
                }
            }
            return isCheck;
        }
    }
}
